package ca.llsutherland.nfl.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ca.llsutherland.nfl.exception.ValidationException;

public class StringUtil {

	public static boolean isNonBlankString(String string) {
		return !isBlankString(string);
	}

	public static boolean isBlankString(String string) {
		return (string == null || string.trim().length() == 0);
	}
	
	public static boolean containsOnlyValidCharacters(String string) throws ValidationException {
		return containsValidCharacters(string);
	}

	private static boolean containsValidCharacters(String string) throws ValidationException {
		isNonBlankString(string);
		Pattern characterPattern = Pattern.compile("^[a-zA-Z_0-9_\\s]*");
		Matcher characterMatcher = characterPattern.matcher(string.trim());
		return characterMatcher.matches();
	}
	
	public static boolean containsAtSymbol(String string) throws ValidationException {
		isNonBlankString(string);
		return string.contains("@");
	}
	
	public static String addSingleQuotes(String string) {
        return "'" + string.replaceAll("'", "''") + "'";
    }

	public static boolean isOnlyNumeric(String string) {
		isNonBlankString(string);
		Pattern characterPattern = Pattern.compile("^[0-9_\\s]*");
		Matcher characterMatcher = characterPattern.matcher(string.trim());
		return characterMatcher.matches();
	}
	
	public static boolean isOnlyAlphabetical(String string) {
		isNonBlankString(string);
		Pattern characterPattern = Pattern.compile("^[a-zA-Z_\\s]*");
		Matcher characterMatcher = characterPattern.matcher(string.trim());
		return characterMatcher.matches();
	}
}
